<?php

function get_module_detail($id) {
    include(dirname(__FILE__) . '/../config.php');
    $con = mysqli_connect($host, $user, $pass, $db);
    mysqli_set_charset($con, "utf8");

    if (mysqli_connect_errno()) {
        die("Failed to connect to MySQL: " . mysqli_connect_error());
    }
    $id = mysql_real_escape_string($id);
    $sql = "SELECT * FROM modules where id=" . $id;

    mysqli_query($con, "SET NAMES 'utf8'");
    $result = mysqli_query($con, $sql);
    if (!$result) {
        die(mysqli_error($con) . ':' . $sql);
    }

    $contact = array();
    while ($row = mysqli_fetch_array($result)) {
        $contact = $row;
    }

    mysqli_close($con);
    return $contact;
}

function get_parent_only($user_id=null) {
    include(dirname(__FILE__) . '/../config.php');
    $con = mysqli_connect($host, $user, $pass, $db);
    mysqli_set_charset($con, "utf8");

    if (mysqli_connect_errno()) {
        die("Failed to connect to MySQL: " . mysqli_connect_error());
    }
    
    $sql = "SELECT * FROM modules where level=0";
    //echo $sql;
    if($user_id!=null){
        $user_id = mysql_real_escape_string($user_id);
        $sql.=' AND id in (SELECT module_id FROM who_can_do_what WHERE user_id='.$user_id.' AND can_view=1)';
    }
    mysqli_query($con, "SET NAMES 'utf8'");
    $result = mysqli_query($con, $sql);
    if (!$result) {
        die(mysqli_error($con) . ':' . $sql);
    }

    $contact = array();
    while ($row = mysqli_fetch_array($result)) {
        $contact[] = $row;
    }

    mysqli_close($con);
    return $contact;
}

function get_children($id) {
    include(dirname(__FILE__) . '/../config.php');
    $con = mysqli_connect($host, $user, $pass, $db);
    mysqli_set_charset($con, "utf8");

    if (mysqli_connect_errno()) {
        die("Failed to connect to MySQL: " . mysqli_connect_error());
    }
    $id = mysql_real_escape_string($id);
    $sql = "SELECT * FROM modules where parent_id=" . $id;
    //echo $sql;
    mysqli_query($con, "SET NAMES 'utf8'");
    $result = mysqli_query($con, $sql);
    if (!$result) {
        die(mysqli_error($con) . ':' . $sql);
    }

    $contact = array();
    while ($row = mysqli_fetch_array($result)) {
        $contact[] = $row;
    }

    mysqli_close($con);
    return $contact;
}

function fetch_children($parent) {

    include(dirname(__FILE__) . '/../config.php');

    $con = mysqli_connect($host, $user, $pass, $db);
    mysqli_set_charset($con, "utf8");
// Check connection
    if (mysqli_connect_errno()) {
        die("Failed to connect to MySQL: " . mysqli_connect_error());
    }

    $sql = 'SELECT id FROM modules WHERE parent_id = "' . (int) $parent . '"';

    //echo $sql;

    mysqli_query($con, "SET NAMES 'utf8'");
    $result = mysqli_query($con, $sql);
    if (!$result) {
        die(mysqli_error($con) . ':' . $sql);
    }

    $list = array();
    while ($row = mysqli_fetch_array($result)) {
        $list[] = (int) $row['id'];
        $list = array_merge($list, fetch_children($row['id']));
    }
    mysqli_close($con);
    return $list;
}

?>